package com;

/**
 * @ClassName MaxVolume
 * @Description 求最大容积
 * @Author shenrui
 * @Date 2021/9/11
 */
public class MaxVolume {
    /**
     * @Description 求最大容积
     * @Author shenrui
     * @Date 2021/9/11
     */
    public int compute(int[] heightList,int heightListLength){
        int left,right,minBucketHeight,temporaryMaxHeight,maxBucketVolume = 0;
        left = 0;
        right = heightListLength;
        while(left < right){
            //求左右两边height较短的一边
            if (heightList[left] > heightList[right]) {
                minBucketHeight = heightList[right];
            } else {
                minBucketHeight = heightList[left];
            }
            //求出此时所围成的的最大容积
            if (maxBucketVolume < minBucketHeight * (right -left))
                maxBucketVolume = minBucketHeight * (right -left);
            //求较短一边height的向中间靠拢的，下一个更长的height。
            if (heightList[left] > heightList[right]) {
                temporaryMaxHeight = right;
                while (heightList[temporaryMaxHeight] <= heightList[right]&&temporaryMaxHeight > left) {
                    temporaryMaxHeight--;
                }
                right = temporaryMaxHeight;
            } else {
                temporaryMaxHeight = left;
                while (heightList[temporaryMaxHeight] <= heightList[left] && temporaryMaxHeight < right) {
                    temporaryMaxHeight++;
                }
                left = temporaryMaxHeight;
            }
        }
        return maxBucketVolume;
    }
}
